package com.example.anydo.util.pub_util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class SPUtil {

    public static String FILE = "sp_file";
    public static final String KEY_ID = "id";
    public static final String KEY_LOGIN = "login";
    public static final String KEY_MUSIC = "music";



    public static void putUserId(Context context, String userId){
        SharedPreferences.Editor editor = context
                .getSharedPreferences(FILE,Context.MODE_PRIVATE)
                .edit();
        editor.putString(KEY_ID,userId);
        editor.commit();
        Log.e("TAG", "putUserId: "+userId );
    }
    public static String getUserId(Context context){
        SharedPreferences sp = context.getSharedPreferences(FILE, Context.MODE_PRIVATE);
        return sp.getString(KEY_ID,"");
    }
    public static boolean putData(Context context, String key, Object value){
        SharedPreferences.Editor editor = context
                .getSharedPreferences(FILE,Context.MODE_PRIVATE)
                .edit();
        String type = value.getClass().getSimpleName();
        if ("String".equals(type)) {
            editor.putString(key, (String) value);
        } else if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) value);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) value);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) value);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) value);
        }
        return editor.commit();
    }

    public static Object getData(Context context,String key,Object defaultValue){
        String type = defaultValue.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(FILE, Context.MODE_PRIVATE);
        if ("String".equals(type)) {
            return sp.getString(key, (String) defaultValue);
        } else if ("Integer".equals(type)) {
            return sp.getInt(key, (Integer) defaultValue);
        } else if ("Boolean".equals(type)) {
            return sp.getBoolean(key, (Boolean) defaultValue);
        } else if ("Float".equals(type)) {
            return sp.getFloat(key, (Float) defaultValue);
        } else if ("Long".equals(type)) {
            return sp.getLong(key, (Long) defaultValue);
        }
        return null;
    }
}
